package com.gxa.ssm251.service.impl;


import com.gxa.ssm251.exception.BusinessException;
import com.gxa.ssm251.mapper.RedeemCodeMapper;
import com.gxa.ssm251.pojo.RedeemCode;
import com.gxa.ssm251.service.RedeemCodeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 舒空
 */

@Service("redeemCodeService")
public class RedeemCodeServiceImpl implements RedeemCodeService {


    @Resource
    private RedeemCodeMapper redeemCodeMapper;



    @Override
    public RedeemCode selectByName(String name) {
        return redeemCodeMapper.selectByName(name);
    }

    @Override

    public Integer addRedeemCode(RedeemCode redeemCode) throws BusinessException {
        RedeemCode redeemCode1 = redeemCodeMapper.selectByName(redeemCode.getName());
        if (redeemCode1 == null) {
            return redeemCodeMapper.insert(redeemCode);
        } else {
            throw new BusinessException("兑换码已存在");
        }
    }

    @Override
    //创建事务
    @Transactional(propagation = Propagation.REQUIRED)

    public Integer addRcScope(Integer id, List<String> list) {
        Integer integer = 0;
        for (String s : list) {
            integer += redeemCodeMapper.insertScope(id, Integer.valueOf(s));
        }
        return integer;
    }

    @Override
    public Integer selectRedeemCodeAll() {
        return redeemCodeMapper.selectRedeemCodeAll();
    }

    @Override
    public List<RedeemCode> selectAllByPage(Integer page, Integer size) {
        return redeemCodeMapper.selectAllByPage(page,size);
    }

  /*

    @Override
    public List<RedeemCode> selectById(Integer id) {
        return redeemCodeMapper.selectById(id);
    }









    @Override
    public Boolean updateInUse(RedeemCode redeemCode) {
        return redeemCodeMapper.updateInUse(redeemCode);
    }

    @Override
    public Integer updateScope(Integer id, List<String> list) {
        Integer integer = redeemCodeMapper.deleteScope(id);
        Integer integer1=0;
        if (integer>0){
            for (String s : list) {
                integer1 += redeemCodeMapper.insertScope(id, Integer.valueOf(s));
            }
        }else {
            System.out.println("删除失败");
        }
        return integer1;
    }

    @Override
    public Integer update(RedeemCode redeemCode) {
        return redeemCodeMapper.update(redeemCode);
    }*/
}
